package com.sphd.education.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.sphd.education.EducationApplication;
import com.sphd.education.R;
import com.sphd.education.base.BaseActivity;
import com.sphd.education.fragments.HomeFragment;
import com.sphd.education.fragments.LessonFragment;
import com.sphd.education.fragments.MessageFragment;
import com.sphd.education.fragments.MineFragment;
import com.sphd.education.info.CityGpsInfo;
import com.sphd.education.info.EducationIntentKey;
import com.sphd.education.info.MyInfo;
import com.sphd.education.interfaces.FragmentInterface;
import com.sphd.education.modle.EducationUrl;


import org.litepal.crud.DataSupport;

import java.util.ArrayList;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* 程序的主界面
* */
public class HomeActivity extends FragmentActivity {
    @Bind(R.id.home_linear)
    LinearLayout homeLinear;
    @Bind(R.id.lessonlinear)
    LinearLayout lessonlinear;
    @Bind(R.id.message_linear)
    LinearLayout messageLinear;
    @Bind(R.id.mine_linear)
    LinearLayout mineLinear;
    @Bind(R.id.home_img)
    ImageView homeImg;
    @Bind(R.id.home_text)
    TextView homeText;
    @Bind(R.id.lesson_img)
    ImageView lessonImg;
    @Bind(R.id.lesson_text)
    TextView lessonText;
    @Bind(R.id.message_img)
    ImageView messageImg;
    @Bind(R.id.message_text)
    TextView messageText;
    @Bind(R.id.mine_img)
    ImageView mineImg;
    @Bind(R.id.mine_text)
    TextView mineText;
    private HomeFragment homeFragment;
    private MessageFragment messageFragment;
    private LessonFragment lessonFragment;
    private MineFragment mineFragment;
    private Context context;
    private Bundle bundle;
    private FragmentInterface fragmentInterface;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;
    private int fragmentFlag = 0;

    private MyInfo myInfo;//我的实体类存储设置的相关信息
    private CityGpsInfo cityGpsInfo;
    private String Tag = "HomeActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ButterKnife.bind(this);
        initView();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void initView() {

        context = HomeActivity.this;
        bundle = new Bundle();
        cityGpsInfo = DataSupport.findLast(CityGpsInfo.class);
        myInfo = DataSupport.findLast(MyInfo.class);
        if (myInfo == null) {
            myInfo = new MyInfo();
            myInfo.setLogon_status(0);
            myInfo.save();
        }
        bundle.putString(EducationIntentKey.CITY, cityGpsInfo.getOthercity());
        fragmentCallBack(1);
    }




    private void fragmentCallBack(int flag) {
        mFragmentManager = HomeActivity.this.getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
//        fragmentList.get(flag - 1).setArguments(bundle);
        hide(mFragmentTransaction);
        if (fragmentFlag != flag) {
            switch (flag) {
                case 1://
                    if (homeFragment==null){
                        homeFragment = HomeFragment.newInstance(bundle);
                        mFragmentTransaction.add(R.id.fragment_layout,homeFragment);
                    }else {
                        mFragmentTransaction.show(homeFragment);
                    }
                    homeFragment.setCallBack(new MyFragmentCallBack());

                    break;
                case 2://
//                    if (lessonFragment==null){
                        lessonFragment = LessonFragment.newInstance(bundle);
                        mFragmentTransaction.add(R.id.fragment_layout, lessonFragment);
//                    }else {
//                        mFragmentTransaction.show(lessonFragment);
//                    }
                    lessonFragment.setCallBack(new MyFragmentCallBack());
                    break;
                case 3://
                    if (messageFragment==null){

                        messageFragment = MessageFragment.newInstance(bundle);
                        mFragmentTransaction.add(R.id.fragment_layout, messageFragment);
                    }else {
                        mFragmentTransaction.show(messageFragment);

                    }

                    messageFragment.setCallBack(new MyFragmentCallBack());

                    break;
                case 4:
                    if (mineFragment==null){

                        mineFragment = MineFragment.newInstance(bundle);
                        mFragmentTransaction.add(R.id.fragment_layout, mineFragment);
                    }else {

                        mFragmentTransaction.show(mineFragment);
                    }
                    mineFragment.setCallBack(new MyFragmentCallBack());

                    break;
            }
            mFragmentTransaction.commit();
            fragmentFlag = flag;
        }

    }

    private void hide(FragmentTransaction transaction) {
        if (homeFragment!=null){
            transaction.hide(homeFragment);
        }
        if (lessonFragment!=null){
            transaction.remove(lessonFragment);
        }
        if (messageFragment !=null){
            transaction.hide(messageFragment);
        }
        if (mineFragment!=null){
            transaction.hide(mineFragment);
        }
    }




    @OnClick(R.id.home_linear)
    public void setOnClickHomeLinear() {
        fragmentCallBack(1);
        clearImg();
        homeImg.setImageResource(R.mipmap.home);
    }

    @OnClick(R.id.lessonlinear)
    public void setOnClickLessonLinear() {
        fragmentCallBack(2);
        clearImg();
        lessonImg.setImageResource(R.mipmap.lesson);
    }

    @OnClick(R.id.message_linear)
    public void setOnClickMessageLinear() {
        fragmentCallBack(3);
        clearImg();
        messageImg.setImageResource(R.mipmap.message);
    }

    @OnClick(R.id.mine_linear)
    public void setOnClickMineLinear() {
        fragmentCallBack(4);
        clearImg();
        mineImg.setImageResource(R.mipmap.mine);
    }

    private void clearImg() {
        homeImg.setImageResource(R.drawable.bg_home);
        lessonImg.setImageResource(R.drawable.bg_lesson);
        messageImg.setImageResource(R.drawable.bg_message);
        mineImg.setImageResource(R.drawable.bg_mine);
    }

    class MyFragmentCallBack implements FragmentInterface {

        @Override
        public void fragmentItemSelected(int position) {
            switch (position) {
                case R.id.city_text:
                    fragmentCallBack(1);
                    break;
                case 1:
//                    fragmentCallBack(1);
//                    homeImg.setImageResource(R.mipmap.home);
//                    clearImg();
                    setOnClickHomeLinear();
                    break;
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("activity", "activityonStop====");

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        EducationUrl.showToast(context, "requestCode=" + requestCode + "resultCode" + resultCode);
        switch (resultCode) {
            case EducationIntentKey.LOGONCODE:
//                EducationUrl.showToast(context, "这是登陆返回的");
                break;
            case EducationIntentKey.REGISTERCODE:
//                EducationUrl.showToast(context, "注册返回");
                break;
            case EducationIntentKey.SEARCHCITY:

                cityGpsInfo = DataSupport.findLast(CityGpsInfo.class);
                if (!cityGpsInfo.getLastcity().equals(cityGpsInfo.getOthercity())) {//切换后的城市和上一个不一样 并且不是定位城市
                    fragmentCallBack(1);
                }
                break;

            default:
                break;
        }

    }
}
